package org.handover.domain;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import org.hibernate.annotations.Parameter;

@Entity
@Table
public class UserInformation implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GenericGenerator(name = "foreignGenerator",
            strategy = "foreign",
            parameters = @Parameter(name = "property", value = "user")
    )
    @GeneratedValue(generator = "foreignGenerator")
    private Long id;

    @OneToOne
    @PrimaryKeyJoinColumn
    private User user;

    @NotNull(message = "{notnull}")
    @Size(min = 2, max = 6, message = "{user.name}")
    @Column(nullable = false)
    private String name;

    @Size(max = 60, message = "{user.address}")
    @Column
    private String address;

    @NotNull(message = "{notnull}")
    @Size(min = 11, message = "{user.telphone}")
    @Column(nullable = false)
    private String telephone;

    @NotNull(message = "{notnull}")
    @Size(min = 18, max = 18, message = "{user.idCard}")
    @Column(name = "id_card", unique = true, nullable = false)
    private String idCard;

    @NotNull(message = "{notnull}")
    @Range(min = 1, max = 999999, message = "{user.jobNumber}")
    @Column(name = "job_number", unique = true, nullable = false)
    private Integer jobNumber;

    @NotNull(message = "{notnull}")
    @Column(name = "morning_date", unique = true, nullable = false)
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date morningDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public Integer getJobNumber() {
        return jobNumber;
    }

    public void setJobNumber(Integer jobNumber) {
        this.jobNumber = jobNumber;
    }

    public Date getMorningDate() {
        return morningDate;
    }

    public void setMorningDate(Date morningDate) {
        this.morningDate = morningDate;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserInformation)) {
            return false;
        }
        UserInformation other = (UserInformation) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.getId().equals(other.getId()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "hanover.domain.UserInformation[ id=" + getId() + " ]";
    }

}
